set more off
clear

do set_path.do

***** Tab A1, A2: Benchmark/Government CIP transition tables *****

global TABLE_FOR_PDF 0

foreach ratetype in govt bench {
	
	*** I. Create transition tables ***
	use "${datadir}cip_all_both.dta", clear
	
	if "`ratetype'"=="govt" {
		gen sofr_cip = sofr_cip_govt
		gen ibor_cip = ibor_cip_govt
		local sym "\Phi"
	}
	else {
		gen ibor_cip = (ibor_r-ibor_r_usd-ibor_rho)*100
		gen sofr_cip = (sofr_r-sofr_r_usd-sofr_rho)*100
		local sym "x"
		keep if inlist(currency,"CAD","CHF","EUR","GBP","JPY","THB")
	}

	keep if date>=mdy(4,1,2018) & date<=mdy(6,30,2024)

	gen cip_diff = sofr_cip - ibor_cip 
	sort date currency
	gen last_ibor = date if ibor_cip != .
	gen first_sofr = date if sofr_cip != .

	*** Keep only transition period  ***
	bys currency tenor: egen max_ibor = max(last_ibor)
	bys currency tenor: egen min_sofr = min(first_sofr)
	drop if (date > max_ibor) | (date < min_sofr)

	*** Calculate statistics ***
	bys currency tenor: egen sofr_ibor_corr = corr(sofr_cip ibor_cip)
	bys currency tenor: egen sofr_sd = sd(sofr_cip)
	bys currency tenor: egen ibor_sd = sd(ibor_cip)
	gen sd_ratio = sofr_sd/ibor_sd

	collapse (mean) sofr_cip ibor_cip cip_diff (p5) p5diff=cip_diff (p95) p95diff=cip_diff (sd) sddiff=cip_diff (max) sofr_ibor_corr sd_ratio last_ibor (first) group (min) first_sofr, by(currency tenor)

	gen overlap = last_ibor - first_sofr
	replace overlap = . if overlap < 0
	sort tenor group currency

	global tablevars currency sofr_cip ibor_cip cip_diff p5diff p95diff sddiff sofr_ibor_corr sd_ratio first_sofr last_ibor overlap
	format sofr_cip ibor_cip cip_diff p5diff p95diff sddiff sofr_ibor_corr sd_ratio %8.2f
	format overlap %9.0fc
	format last_ibor first_sofr %tdNN/DD/YY

	*** II. Output transition tables ***
	levelsof tenor, local(tenors)
	foreach tenor of local tenors {
		if ${TABLE_FOR_PDF} {
			local headtext = "\begin{landscape} \begin{table} \centering \caption [`tenor' transition table] {`tenor' transition table} \label{table`tenor'} 	\begin{adjustbox}{width=1.75\textwidth} \begin{tabular}{l r r r r r r r r r r r} \\ \midrule & \$\overline{`sym'_t^{\text{SOFR}}}$ & \$\overline{`sym'_t^{\text{IBOR}}}$ & \$\overline{\hat{`sym'}_t}$ & \$\hat{`sym'}_t^{\text{5th pct.}}$ & \$\hat{`sym'}_t^{\text{95th pct.}}$ & $\sigma_{\hat{`sym'}_t}$ & $\rho(`sym'_t^{\text{SOFR}},`sym'_t^{\text{IBOR}})$ & \$\frac{\sigma_{`sym'_t^{\text{SOFR}}}}{\sigma_{`sym'_t^{\text{IBOR}}}}$ & First SOFR & Last IBOR & Days \\ \midrule \textbf{G10} \\"

			local foottext = "\midrule \end{tabular} \end{adjustbox} \end{table} \end{landscape}"
		}
		else {
			local headtext = "\begin{tabular}{l r r r r r r r r r r r} \\ \multicolumn{12}{c}{\textbf{`tenor' tenor}}\\ \midrule & \$\overline{`sym'_t^{\text{SOFR}}}$ & \$\overline{`sym'_t^{\text{IBOR}}}$ & \$\overline{\hat{`sym'}_t}$ & \$\hat{`sym'}_t^{\text{5th}}$ & \$\hat{`sym'}_t^{\text{95th}}$ & $\sigma_{\hat{`sym'}_t}$ & $\rho(`sym'_t^{\text{SOFR}},`sym'_t^{\text{IBOR}})$ & \$\frac{\sigma_{`sym'_t^{\text{SOFR}}}}{\sigma_{`sym'_t^{\text{IBOR}}}}$ & First SOFR & Last IBOR & Days \\ \midrule \textbf{G10} \\"
			
			local foottext = "\midrule \end{tabular}"
		}

		listtab $tablevars using "${outdir}tables/trans_table_`ratetype'_`tenor'.tex" if tenor == "`tenor'" & group == "g10", rstyle(tabular) head("`headtext'") replace
		listtab $tablevars if tenor == "`tenor'" & group == "eme", head("\\ \textbf{EMs} \\") foot("`foottext'") rstyle(tabular) appendto("${outdir}tables/trans_table_`ratetype'_`tenor'.tex")
	}
}
